/**
 * ReportGear(2011)
 */
package com.reportgear.designer.editor.object;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import net.abbp.core.util.StringUtils;
import com.reportgear.designer.ui.base.JNumberField;

/**
 * 数字编辑器
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class NumberEditor extends Editor {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -1271661774045714817L;
	private JNumberField numberField;
	private String oldValue;
	private boolean isInteger;
	private boolean isFloat;

	public NumberEditor() {
		this(null);
	}

	public NumberEditor(Object value) {
		this(value, "");
	}

	public NumberEditor(Object value, String name) {
		this.oldValue = "";
		this.isInteger = false;
		this.isFloat = false;

		setLayout(new BorderLayout());
		this.numberField = new JNumberField();
		add(this.numberField, BorderLayout.CENTER);
		this.numberField.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent evt) {
			}

			public void keyPressed(KeyEvent evt) {
			}

			public void keyReleased(KeyEvent evt) {
				int keyCode = evt.getKeyCode();
				if (keyCode == KeyEvent.VK_ESCAPE) {
					numberField.setText(NumberEditor.this.oldValue);
				} else {
					if (keyCode != KeyEvent.VK_ENTER) {
						return;
					}
					fireEditingStopped();
				}
			}
		});

		this.numberField.setHorizontalAlignment(JNumberField.RIGHT);
		setValue(value);
		setName(name);
	}

	public void setColumns(int columsn) {
		this.numberField.setColumns(columsn);
	}

	public int getHorizontalAlignment() {
		return this.numberField.getHorizontalAlignment();
	}

	public void setHorizontalAlignment(int align) {
		this.numberField.setHorizontalAlignment(align);
	}

	public Object getValue() {
		if (this.isInteger) {
			return new Integer((int) this.numberField.getValue());
		}

		if (this.isFloat) {
			return new Float(this.numberField.getValue());
		}

		return new Double(this.numberField.getValue());
	}

	public void setValue(Object value) {
		this.numberField.setText("");
		if ((value == null) || (!(value instanceof Number))) {
			value = new Integer(0);
		}

		if (value instanceof Double) {
			this.numberField.setInteger(false);
			this.numberField.setValue(((Double) value).doubleValue());
			this.isInteger = false;
			this.isFloat = false;
		} else if (value instanceof Float) {
			this.numberField.setInteger(false);
			this.numberField.setValue(((Float) value).doubleValue());
			this.isInteger = false;
			this.isFloat = true;
		} else if (value instanceof Integer) {
			this.numberField.setInteger(true);
			this.numberField.setValue(((Integer) value).intValue());
			this.isInteger = true;
			this.isFloat = false;
		} else {
			this.numberField.setInteger(false);
			this.numberField.setValue(((Number) value).doubleValue());
			this.isInteger = false;
			this.isFloat = false;
		}

		this.oldValue = StringUtils.convertToString(value);
	}

	public void setType(Object value) {
		if ((value == null) || (!(value instanceof Number))) {
			value = new Double(0.0D);
		}
		if (value instanceof Double) {
			this.numberField.setInteger(false);
			this.isInteger = false;
			this.isFloat = false;
		} else if (value instanceof Float) {
			this.numberField.setInteger(false);
			this.isInteger = false;
			this.isFloat = true;
		} else if (value instanceof Integer) {
			this.numberField.setInteger(true);
			this.isInteger = true;
			this.isFloat = false;
		} else {
			this.numberField.setInteger(false);
			this.isInteger = false;
			this.isFloat = false;
		}
	}

	public void setEnabled(boolean paramBoolean) {
		this.numberField.setEnabled(paramBoolean);
	}

	public void requestFocus() {
		this.numberField.requestFocus();
	}

	public boolean accept(Object paramObject) {
		return ((paramObject != null) && (getValue().getClass().getName().equals(paramObject.getClass().getName())));
	}
}